<?php
// +----------------------------------------------------------------------
// | CSMITSM  [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2024 https://www.fastadmin.net All rights reserved.
// +----------------------------------------------------------------------
// | Author: chenshiming
// +----------------------------------------------------------------------

namespace app\admin\controller\csmitsm;

use addons\csmitsm\library\xcore\xcore\XcException;
use addons\csmitsm\library\xcore\xcore\base\XcABackend;
use addons\csmitsm\library\xcore\xcore\utils\XcDaoUtils;
use addons\csmitsm\library\xcore\xcore\utils\XcRequestUtils;
use addons\csmitsm\library\xcore\xcore\utils\XcAdminTabUtils;
use addons\csmitsm\library\xcore\xcore\utils\XcDatabaseUtils;
use addons\csmitsm\library\xcore\xcore\utils\XcResponseUtils;
use addons\csmitsm\library\xcore\xcflow\utils\XcformPageUtils;
use addons\csmitsm\library\xcore\xcore\utils\XcTransactionUtils;
use addons\csmitsm\library\xcore\xcflow\utils\XcflowallformUtils;

/**
 * 页面视图
 *
 * @icon fa fa-circle-o
 */
class Xcflowviewpageview extends XcABackend
{

    /**
     * Xcflowviewpageview模型对象
     * @var \app\admin\model\csmitsm\Xcflowviewpageview
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\csmitsm\Xcflowviewpageview;
        $this->assign("statusList", $this->model->getStatusList());

        // csmitsm:设置页面tabs
        $xc_tabid = XcRequestUtils::param("page_id");
        XcAdminTabUtils::append_control_initialize('xcflowviewpage', $xc_tabid, __CLASS__, $this->view);
    }



    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */


    public function configview()
    {
        $view_id = XcRequestUtils::get("view_id");
        $page_id = XcRequestUtils::get("page_id", true);

        $views = XcformPageUtils::getViewsByPageid($page_id);
        $view = XcformPageUtils::getViewIfNullThenFirstView($page_id, $view_id);

        // $tables = Db::table(XcDatabaseUtils::appendTablePrefix("csmitsm_xcflowform"))->select();
        $tables = XcflowallformUtils::queryTableinfoList();

        $this->assignconfig("view_id", $view ? $view->id : null);
        $this->assignconfig("page_id", $page_id);
        $this->assignconfig("views", $views);
        $this->assignconfig("view", $view);
        $this->assignconfig("tables", $tables);

        return $this->fetch();
    }

    public function addConfigview()
    {
        $page_id = XcRequestUtils::get("page_id");
        $row = XcDaoUtils::create($this->model, [
            'xcflowviewpage_id' => $page_id,
            'name' => '视图',
            'configjson' => '[]',
            'weigh' => 0
        ]);
        XcResponseUtils::redirect("csmitsm/xcflowviewpageview/configview?view_id={$row->id}&page_id={$page_id}");
    }
    public function deleteConfigview()
    {
        $view_id = XcRequestUtils::get("view_id");
        $view = XcDaoUtils::getRowById($this->model, $view_id);
        XcTransactionUtils::service(function () use ($view) {
            // can not delete last view
            if (true) {
                $count = XcDaoUtils::getCountBySql(new \app\admin\model\csmitsm\Xcflowviewpageview(), [
                    ["xcflowviewpage_id", "=", $view->xcflowviewpage_id]
                ]);
                if ($count == 1) {
                    throw new XcException("无法删除最后一个视图");
                }
            }
            $view->save(['status' => 'hidden']);
        });
        XcResponseUtils::redirect("csmitsm/xcflowviewpageview/configview?page_id={$view->xcflowviewpage_id}");
    }

    private function _getFilterfields()
    {
        $datas = XcRequestUtils::post("filterfields/a");
        if ($datas) {
            foreach ($datas as &$item) {
                $item['el'] = isset($item['el']) ? trim($item['el']) : null;
            }
        }
        return $datas;
    }

    public function savesubmit()
    {
        $view_id = XcRequestUtils::post("view_id", true);
        $page_id = XcRequestUtils::post("page_id", false);
        $name = XcRequestUtils::post("name");
        $tablename = XcRequestUtils::post("tablename");
        $filtersql = XcRequestUtils::post("filtersql");
        $model = [
            'xcflowviewpage_id' => $page_id,
            'name' => $name,
            'tablename' => $tablename,
            'configjson' => XsJsonUtils::jsonToString([
                'showfields' => XcRequestUtils::post("showfields/a"),
                'filterfields' => $this->_getFilterfields(),
                'filtersql' => $filtersql,
                'orderbyfields' => XcRequestUtils::post("orderbyfields/a"),
            ]),
            // 'weigh' => 0,
        ];
        $tt = XcRequestUtils::post("filterfields/a");
        $model['configjson'] = empty($model['configjson']) ? '[]' : $model['configjson'];

        $row = XcTransactionUtils::service(function () use ($view_id, $model) {
            $row = null;
            if (empty($view_id)) {
                $row = XcDaoUtils::create($this->model, $model);
            } else {
                $row = XcDaoUtils::getRowById($this->model, $view_id);
                $row->save($model);
            }
            return  $row;
        });

        $this->success(null, null, ["row" => $row]);
    }

    public function queryFieldsByTableName()
    {
        $tablename = XcRequestUtils::get("tablename", true);
        $list = XcDatabaseUtils::queryTablefieldList($tablename);
        $this->success(null, null, ["list" => $list]);
    }

    public function getXcformjsonByTablename()
    {
        $tablename = XcRequestUtils::get("tablename", true);
        $xcfieldjsonObj = XcformPageUtils::getXcfieldjsonObjByTablename($tablename);

        $this->success(null, null, ['xcfieldjsonObj' => $xcfieldjsonObj]);
    }
    public function getTableDatasByTablename()
    {
        $tablename = XcRequestUtils::get("tablename", true);
        $table = XcflowallformUtils::getTableinfoByTablename($tablename);

        //$list = Db::table($table['tablename'])->field("id," . $table["displayfieldcode"]." label")->select();
        $list = XcDatabaseUtils::getListBySql($table['tablename'], [], "id," . $table["displayfieldcode"] . " label");
        $this->success(null, null, ['list' => $list]);
    }

}
